package demo7;

public class Queue {
    static class  Node{
        public int val;
        public Node prev;
        public Node next;
        public Node(int val){
            this.val=val;
        }
    }
    Node first=null;
    Node last=null;
    int usedSize=0;
    //尾插入队
    public void offer(int val){
        Node node=new Node(val);
        if(isEmpty()){
            first=last=node;
        }else{
            node.next=last.next;
            last.next=node;
            node.prev=last;
            last=last.next;
        }
        usedSize++;
    }
    //头删出队
    public int poll(){
        if(isEmpty()){
            System.out.println("队为空！！！");
            return -1;
        }
        int val= first.val;;
        first=first.next;
        if(first!=null){
            first.prev=null;
        }
        System.out.println("删除成功！！！");
        return val;
    }
    //获取头节点
    public int peek(){
        if(isEmpty()){
            System.out.println("队为空！！！");
            return -1;
        }
        return first.val;
    }
    public boolean isEmpty(){
        return usedSize==0;
    }
    //打印队中元素
    public void display(){
        Node cur=first;
        if(isEmpty()){
            System.out.println("队为空！！！");
            return;
        }
        while(cur!=null)
        {
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
}
